﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TMWS.Utilities
{
	/// <summary>
	/// TrackMania string extensions
	/// </summary>
	public static class TMStringExtensions
	{
		/// <summary>
		/// Strips the TM color codes
		/// </summary>
		/// <param name="text">Text to strip</param>
		/// <returns>String stripped of TM color codes</returns>
		public static string StripTMCodes(this string text)
		{
			string final = String.Empty;
			string color = "0123456789abcdefABCDEF";
			int index = 0;
			char dollar = '$';

			while (index < text.Length)
			{
				// Matching character is '$'
				if (text[index] == dollar)
				{
					if (index >= text.Length - 1)
					{
						// Return
						return final;
					}

					// We have a color
					if (color.IndexOf(text[index + 1]) >= 0)
					{
						// Increment 3 characters (skip ahead to next code)
						index += 3;
					}

					else
					{
						// Increment character index
						index += 1;
					}
				}

				else
				{
					// Append stripped characters to final result
					final += text[index];
				}

				// Increment index
				index += 1;
			}

			return final;
		}
	}
}